package com.xmall.pethouse_api.service.impl;

import com.xmall.pethouse_api.mapper.ShopMapper;
import com.xmall.pethouse_api.pojo.entity.ShopProduct;
import com.xmall.pethouse_api.pojo.entity.ShopCart;
import com.xmall.pethouse_api.pojo.entity.ShopOrder;
import com.xmall.pethouse_api.pojo.entity.ShopOrderItem;
import com.xmall.pethouse_api.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    // 商品
    @Override
    public List<ShopProduct> listProducts() { return shopMapper.listProducts(); }
    @Override
    public ShopProduct getProductById(Long id) { return shopMapper.getProductById(id); }
    @Override
    public void addProduct(ShopProduct product) {
        product.setCreatedAt(LocalDateTime.now());
        product.setUpdatedAt(LocalDateTime.now());
        shopMapper.insertProduct(product);
    }
    @Override
    public void updateProduct(ShopProduct product) {
        product.setUpdatedAt(LocalDateTime.now());
        shopMapper.updateProduct(product);
    }
    @Override
    public void deleteProduct(Long id) { shopMapper.deleteProduct(id); }

    // 购物车
    @Override
    public List<ShopCart> listCartByUserId(Long userId) { return shopMapper.listCartByUserId(userId); }
    @Override
    public void addCartItem(Long userId, Long productId, Integer quantity) {
        ShopCart cart = shopMapper.getCartItem(userId, productId);
        if (cart == null) {
            cart = new ShopCart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cart.setCreatedAt(LocalDateTime.now());
            cart.setUpdatedAt(LocalDateTime.now());
            shopMapper.insertCartItem(cart);
        } else {
            cart.setQuantity(cart.getQuantity() + quantity);
            cart.setUpdatedAt(LocalDateTime.now());
            shopMapper.updateCartItem(cart);
        }
    }
    @Override
    public void updateCartItem(Long cartId, Integer quantity) {
        ShopCart cart = new ShopCart();
        cart.setId(cartId);
        cart.setQuantity(quantity);
        cart.setUpdatedAt(LocalDateTime.now());
        shopMapper.updateCartItem(cart);
    }
    @Override
    public void deleteCartItem(Long cartId) { shopMapper.deleteCartItem(cartId); }
    @Override
    public void clearCart(Long userId) { shopMapper.clearCartByUserId(userId); }

    @Override
    public List<com.xmall.pethouse_api.pojo.vo.CartVO> listCartVOByUserId(Long userId) {
        return shopMapper.listCartVOByUserId(userId);
    }

    // 订单
    @Override
    public List<ShopOrder> listOrdersByUserId(Long userId) { return shopMapper.listOrdersByUserId(userId); }
    @Override
    public ShopOrder getOrderById(Long id) { return shopMapper.getOrderById(id); }
    @Override
    public void createOrder(Long userId, Long addressId, String remark) {
        // 1. 查询购物车
        List<ShopCart> cartList = shopMapper.listCartByUserId(userId);
        if (cartList == null || cartList.isEmpty()) return;
        // 2. 计算总价
        double total = 0;
        for (ShopCart cart : cartList) {
            ShopProduct product = shopMapper.getProductById(cart.getProductId());
            total += product.getPrice() * cart.getQuantity();
        }
        // 3. 创建订单
        ShopOrder order = new ShopOrder();
        order.setOrderNo("SHOP" + System.currentTimeMillis());
        order.setUserId(userId);
        order.setAddressId(addressId);
        order.setTotalAmount(total);
        order.setStatus(0);
        order.setRemark(remark);
        order.setCreatedAt(LocalDateTime.now());
        order.setUpdatedAt(LocalDateTime.now());
        shopMapper.insertOrder(order);
        // 4. 创建订单明细
        for (ShopCart cart : cartList) {
            ShopOrderItem item = new ShopOrderItem();
            item.setOrderId(order.getId());
            item.setProductId(cart.getProductId());
            item.setQuantity(cart.getQuantity());
            item.setPrice(shopMapper.getProductById(cart.getProductId()).getPrice());
            item.setCreatedAt(LocalDateTime.now());
            item.setUpdatedAt(LocalDateTime.now());
            shopMapper.insertOrderItem(item);
        }
        // 5. 清空购物车
        shopMapper.clearCartByUserId(userId);
    }
    @Override
    public List<ShopOrderItem> listOrderItemsByOrderId(Long orderId) { return shopMapper.listOrderItemsByOrderId(orderId); }
} 